package com.irssproject.app.web.action.lectorRSS;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.irssproject.app.entitats.Item;
import com.irssproject.app.entitats.Reading;
import com.irssproject.app.entitats.User;
import com.irssproject.app.service.lectorRSS.iface.SubscriptionsService;
import com.irssproject.app.utils.Constants;
import com.irssproject.app.web.action.JSONWrappers.ItemJSON;
import com.irssproject.app.web.action.JSONWrappers.ReadingJSON;
import com.irssproject.app.web.action.JSONWrappers.SubscriptionJSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.logging.Logger;

public class ShowSubscriptionAction extends ActionSupport {

    private static final long serialVersionUID = 4960477602907077434L;
    private String feedURL;
    private List<ReadingJSON> lectures;
    private Long idSubscripcio;
    private Integer toElement;
    Logger log = Logger.getLogger(ShowSubscriptionAction.class.getName());

    public Integer getToElement() {
        return toElement;
    }

    public void setToElement(Integer toElement) {
        this.toElement = toElement;
    }

    public Long getIdSubscripcio() {
        return idSubscripcio;
    }

    public void setIdSubscripcio(Long idSubscripcio) {
        this.idSubscripcio = idSubscripcio;
    }
    private String queMostrar;
    SubscriptionsService subscripcioService;
    Map<String, Object> session;

    public ShowSubscriptionAction(SubscriptionsService subscripcioService) {
        this.subscripcioService = subscripcioService;
    }

    public List<ReadingJSON> getLectures() {
        return lectures;
    }

    public void setLectures(List<ReadingJSON> lectures) {
        this.lectures = lectures;
    }

    public String getFeedURL() {
        return feedURL;
    }

    public void setFeedURL(String feedURL) {
        this.feedURL = feedURL;
    }

    public String getQueMostrar() {
        return queMostrar;
    }

    public void setQueMostrar(String queMostrar) {
        this.queMostrar = queMostrar;
    }

    public String mostrarSubscripcio() {

        try {
            session = ActionContext.getContext().getSession();

            List<Reading> rdngsLst = new ArrayList<Reading>();
            Item item;
            ReadingJSON lJSON;
            ItemJSON iJSON;
            SubscriptionJSON sJSON;
            List<ReadingJSON> llistaReturn = new ArrayList<ReadingJSON>();

            if (queMostrar.equals("0")) {
                rdngsLst = subscripcioService.showReadingsOnInterval(idSubscripcio, toElement);
                if (rdngsLst != null && !rdngsLst.isEmpty()) {

                    for (Reading rdng : rdngsLst) {

                        if (!rdng.isRead()) {
                            lJSON = new ReadingJSON();
                            lJSON.setId(rdng.getId());
                            lJSON.setLlegida(false);
                            lJSON.setValorada(rdng.isLiked());

                            iJSON = new ItemJSON();
                            item = rdng.getItem();
                            iJSON.setId(item.getId());
                            iJSON.setPublishedDate(item.getPublishedDate());
                            iJSON.setDataPublicacioDisplay(item.getDataPublicacioString());
                            iJSON.setContent(item.getContent());
                            iJSON.setLink(item.getLink());
                            iJSON.setTitle(item.getTitle());
                            lJSON.setItem(iJSON);

                            sJSON = new SubscriptionJSON();
                            sJSON.setId(idSubscripcio);

                            lJSON.setSubscripcio(sJSON);

                            llistaReturn.add(lJSON);
                        }
                    }
                }

            } else if (queMostrar.equals("1")) {
                rdngsLst = subscripcioService.showAllReadingsOnInterval(idSubscripcio, toElement);
                if (rdngsLst != null && !rdngsLst.isEmpty()) {

                    for (Reading rdng : rdngsLst) {
                        lJSON = new ReadingJSON();
                        lJSON.setId(rdng.getId());
                        lJSON.setLlegida(rdng.isRead());
                        lJSON.setValorada(rdng.isLiked());

                        iJSON = new ItemJSON();
                        item = rdng.getItem();
                        iJSON.setId(item.getId());
                        iJSON.setPublishedDate(item.getPublishedDate());
                        iJSON.setDataPublicacioDisplay(item.getDataPublicacioString());
                        iJSON.setContent(item.getContent());
                        iJSON.setLink(item.getLink());
                        iJSON.setTitle(item.getTitle());
                        lJSON.setItem(iJSON);

                        sJSON = new SubscriptionJSON();
                        sJSON.setId(idSubscripcio);

                        lJSON.setSubscripcio(sJSON);

                        llistaReturn.add(lJSON);
                    }
                }
            }

            setLectures(llistaReturn);

        } catch (Exception e) {
            log.severe(e.getMessage());
            addActionError(getText("error.mostrarFeedAction.errorGeneric" + feedURL));
            return ERROR;
        }

        return SUCCESS;
    }

    public String marcarTotesLecturesLlegides() {

        subscripcioService.setAllReadingsRead(getIdSubscripcio());
        return SUCCESS;
    }

    public String mostrarItemsVotats() {

        try {
            session = ActionContext.getContext().getSession();
            User usuari = ((User) session.get(Constants.SESSIO_USUARI_AUTENTICAT));

            Item item;
            ReadingJSON lJSON;
            ItemJSON iJSON;
            SubscriptionJSON sJSON;
            List<ReadingJSON> llistaReturn = new ArrayList<ReadingJSON>();

            List<Reading> rdngsLst = subscripcioService.showLikedReadingsOnInterval(usuari, toElement);
            if (rdngsLst != null && !rdngsLst.isEmpty()) {
                for (Reading rdng : rdngsLst) {
                    lJSON = new ReadingJSON();
                    lJSON.setId(rdng.getId());
                    lJSON.setLlegida(rdng.isRead());
                    lJSON.setValorada(rdng.isLiked());

                    iJSON = new ItemJSON();
                    item = rdng.getItem();
                    iJSON.setId(item.getId());
                    iJSON.setPublishedDate(item.getPublishedDate());
                    iJSON.setContent(item.getContent());
                    iJSON.setLink(item.getLink());
                    iJSON.setTitle(item.getTitle());
                    lJSON.setItem(iJSON);

                    sJSON = new SubscriptionJSON();
                    sJSON.setId(idSubscripcio);

                    lJSON.setSubscripcio(sJSON);

                    llistaReturn.add(lJSON);
                }
            }
            setLectures(llistaReturn);

        } catch (Exception e) {
            log.severe(e.getMessage());
            addActionError(getText("error.mostrarFeedAction.errorGeneric" + feedURL));
            return ERROR;
        }

        return SUCCESS;
    }
}
